/****************************************************************
 *  Original name ColumnPickerButton
 *  Original made by Doctor Felix Woo.
 *	Evolution by Alan,to be a part of megatron.
 *  Birth Date: 11-08-2007
 *	Evolution Date: 05-29-2008
 ****************************************************************/

jsx3.Class.defineClass(
  "ux.megatron.gui.ColumnSwitcherTrigger",
  jsx3.gui.Block,
  null,
  function(control) {
    control.prototype.init = function(strName,intLeft,intTop,intWidth,strText) {
      this.jsxsuper(strName,intLeft,intTop,intWidth,strText);
    }

    control.prototype.getTargetMatrixName = function() {
      return this.targetMatrixName;
    }

    control.prototype.setTargetMatrixName = function(targetMatrixName) {
      delete this.targetObject;
      this.targetMatrixName = targetMatrixName;
    }

    control.prototype.setExclusions = function(listUneditableCols, listExcludedCols) {
      this.listUneditable = listUneditableCols;
      this.listExcluded = listExcludedCols;
    }
    
    control.prototype.setUneditableColumns = function(strColumns) {
        this.uneditable = strColumns;
    }
    
    control.prototype.getUneditableColumns = function() {
        return this.uneditable;
    }
    
    control.prototype.setExcludedColumns = function(strColumns) {
        this.excluded = strColumns;
    }
    
    control.prototype.getExcludedColumns = function() {
        return this.excluded;
    }
    
	control.prototype.loadColumnPicker = function() {
		if(this.cp) {
			this.cp.actuallyClose();
			return;
		}
		if(!this.targetObject) {
			if(!this.targetMatrixName) {
				alert("Please specify the target matrix.");
				return;
			}
			this.targetObject = ux.megatron.Main.getServer().getJSXByName(this.targetMatrixName);
			if(!this.targetObject) {
				try {
					var vars = new Object();
					vars["objJSX"] = this;
					this.targetObject = jsx3.eval(this.targetMatrixName, vars);
				}catch(ex) {
					ux.megatron.Main.logException(ex);
				}
			}
			if(!this.targetObject) {
				alert("Cannot find the target object.");
				return;
			}
			if(!(this.targetObject instanceof jsx3.gui.Matrix)) {
				delete this.targetObject;
				alert("The target object is not an instance of jsx3.gui.Matrix.");
				return;
			}
		}
        
        if(this.uneditable && !this.listUneditable) {
            this.listUneditable = this.uneditable.split(",");
            for(var i=0; i<this.listUneditable.length; i++) {
                this.listUneditable[i] = this.targetObject.getDescendantOfName(this.listUneditable[i]).getId();
            }
            this.listUneditable = new jsx3.util.List(this.listUneditable);
        }
        
        if(this.excluded && !this.listExcluded) {
            this.listExcluded = this.excluded.split(",");
            for(var i=0; i<this.listExcluded.length; i++) {
                this.listExcluded[i] = this.targetObject.getDescendantOfName(this.listExcluded[i]).getId();
            }
            this.listExcluded = new jsx3.util.List(this.listExcluded);
        }
        
		this.cp = ux.megatron.Main.getServer().getRootBlock().load("jsxaddin://user!Megatron/components/columnSwitcher.xml");
		this.cp.initialize(this, this.targetObject, this.listUneditable, this.listExcluded);
	}

    control.prototype.columnPickerClosed = function(obj) {
        if(obj == this.cp) {
            var changed = this.cp.getChangedColumns();
            var me = this;
            try {
                this.doEvent("onpickerclose", {objBUTTON:me,objMATRIX:me.cp.mtxTarget,arrCOLUMNSADDED:changed.added,arrCOLUMNSREMOVED:changed.removed});
            } catch(ex) {
                ux.megatron.Main.logException(ex);
            }
            delete this.cp;
        }
    }
  }
);